package light.head.dao.inv;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;

import light.head.constant.Parm;
import light.head.enm.RESULT;
import light.head.framework.base.BaseDAO;
import light.head.framework.entity.PageBean;
import light.head.framework.utils.DateUtils;
import light.head.model.inv.InvBusiness;
@Service
public class InvBusinessDAO extends BaseDAO<InvBusiness, Serializable>{
	
	
	public RESULT findByMap(Integer oc, Map<String, String> map, PageBean pb){
		if(oc == null) return null;
		
		StringBuffer hql = new StringBuffer("FROM InvBusiness ib WHERE ib.oc = ?");
		List<Object> list = new ArrayList<Object>();
		
		list.add(oc);
		if (null != map) {
			String name = map.get(Parm.NAME);				//单位名称
			String sid = map.get("sid");					//编号
			String stn = map.get("stn");					//类型
			String cn  = map.get(Parm.CN);					//创建人
			String on = map.get(Parm.ON);
			String time1 = map.get(Parm.TIME1);
			String time2 = map.get(Parm.TIME2);
			String type = map.get(Parm.TYPE);				//类别
			String deleted = map.get(Parm.DELETED);
			if (isNotNull(name)) {
				list.add("%" + name + "%");
				hql.append(" AND ib.name LIKE ?");
			}
			if (isNotNull(sid)) {
				list.add("%" + sid + "%");
				hql.append(" AND ib.sid LIKE ?");
			}
			if (isNotNull(stn)) {
				list.add("%" + stn + "%");
				hql.append(" AND ib.stc IN (SELECT u.code FROM InvBusinessType u WHERE u.name LIKE ?)");
			}
			if (isNotNull(cn)) {
				list.add("%" + cn + "%");
				hql.append(" AND ib.cu IN (SELECT u.code FROM UserInfo u WHERE u.realName LIKE ?)");	
			}
			if (isNotNull(on)) {
				list.add("%" + on + "%");
				hql.append(" AND ib.oc IN (SELECT o.code FROM Organ o WHERE o.name LIKE ?)");
			}
			if (isNotNull(time1)) {
				list.add(DateUtils.getDateByStr(time1, null));
				hql.append(" AND ib.ct >= ?");
			}
			if (isNotNull(time2)) {
				list.add(DateUtils.getDateByStr(time2, null));
				hql.append(" AND ib.ct >= ?");
			}
			if (isNotNull(type)) {
				list.add(Byte.parseByte(type));
				hql.append(" AND ib.type = ?");
			}
			if (isNotNull(deleted)) {
				list.add(Boolean.parseBoolean(deleted));
				hql.append(" AND ib.deleted = ?");
			}
		}
		hql.append(" ORDER BY ib.code DESC");
		return findToPageBean(hql.toString(), pb, list);
		
	}
	
	public RESULT update(InvBusiness ib) {
		if (null == ib || ib.getCode() == null) return null;
		InvBusiness db = this.get(ib.getCode());
		if (null == db) return RESULT.NOT_FIND;
		
		boolean ok = false;
		if (!isNotNull(db.getName()) && isNotNull(ib.getName()) || (!db.getName().equals(ib.getName()))) {
			db.setName(ib.getName());
			ok = true;
		}
		if (!isNotNull(db.getPhone()) && isNotNull(ib.getPhone()) || (!db.getPhone().equals(ib.getPhone()))) {
			db.setPhone(ib.getPhone());
			ok = true;
		}
		if (!isNotNull(db.getSid()) && isNotNull(ib.getSid()) || (!db.getSid().equals(ib.getSid()))) {
			db.setSid(ib.getSid());
			ok = true;
		}
		if (!isNotNull(db.getAddress()) && isNotNull(ib.getAddress()) || (!db.getAddress().equals(ib.getAddress()))) {
			db.setAddress(ib.getAddress());
			ok = true;
		}
		if (!isNotNull(db.getEmail()) && isNotNull(ib.getEmail()) || (!db.getEmail().equals(ib.getEmail()))) {
			db.setEmail(ib.getEmail());
			ok = true;
		}
		if (!isNotNull(db.getFax()) && isNotNull(ib.getFax()) || (!db.getFax().equals(ib.getFax()))) {
			db.setFax(ib.getFax());
			ok = true;
		}if (!isNotNull(db.getStn()) && isNotNull(ib.getStn()) || (!db.getStn().equals(ib.getStn()))) {
			db.setStn(ib.getStn());
			ok = true;
		}
		if (db.getStc() != null && ib.getStc()  != null || (db.getStc() != ib.getStc())) {
			db.setStc(ib.getStc());
			ok = true;
		}if (db.getType() != null && ib.getType()  != null || (db.getType() != ib.getType())) {
			db.setType(ib.getType());
			ok = true;
		}
		if (ok)
			return super.upd(db);
		
		return RESULT.SUCCESS;
	}

}
